package com.gitee.wsl.io.sink

import kotlinx.io.Buffer
import kotlinx.io.InternalIoApi
import kotlinx.io.RawSink
import kotlinx.io.RawSource
import kotlinx.io.Sink
import kotlinx.io.buffered

open class ForwardingSink(val srcSink: Sink): RawSink {

    @InternalIoApi
    val buffer: Buffer = srcSink.buffer

    fun transferFrom(source: RawSource): Long = srcSink.transferFrom(source)

    fun write(source: RawSource, byteCount: Long) = srcSink.write(source, byteCount)

    /**
     * Writes a byte to this sink.
     *
     * @param byte the byte to be written.
     *
     * @throws IllegalStateException when the sink is closed.
     * @throws IOException when some I/O error occurs.
     *
     * @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.writeByte
     */
     fun writeByte(byte: Byte) = srcSink.writeByte(byte)

    /**
     * Writes two bytes containing [short], in the big-endian order, to this sink.
     *
     * @param short the short integer to be written.
     *
     * @throws IllegalStateException when the sink is closed.
     * @throws IOException when some I/O error occurs.
     *
     * @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.writeShort
     */
     fun writeShort(short: Short)= srcSink.writeShort(short)

    /**
     * Writes four bytes containing [int], in the big-endian order, to this sink.
     *
     * @param int the integer to be written.
     *
     * @throws IllegalStateException when the sink is closed.
     * @throws IOException when some I/O error occurs.
     *
     * @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.writeInt
     */
     fun writeInt(int: Int)= srcSink.writeInt(int)

    /**
     * Writes eight bytes containing [long], in the big-endian order, to this sink.
     *
     * @param long the long integer to be written.
     *
     * @throws IllegalStateException when the sink is closed.
     * @throws IOException when some I/O error occurs.
     *
     * @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.writeLong
     */
     fun writeLong(long: Long) = srcSink.writeLong(long)


    override fun close() = srcSink.close()

    override fun flush() = srcSink.flush()

    override fun write(source: Buffer, byteCount: Long) = srcSink.write(source,byteCount)


    /**
     * Writes all buffered data to the underlying sink if one exists.
     * The underlying sink will not be explicitly flushed.
     *
     * This method behaves like [flush], but has weaker guarantees.
     * Call this method before a buffered sink goes out of scope so that its data can reach its destination.
     *
     * @throws IllegalStateException when the sink is closed.
     * @throws IOException when some I/O error occurs.
     *
     * @sample kotlinx.io.samples.KotlinxIoCoreCommonSamples.emit
     */
     fun emit() = srcSink.emit()

    /**
     * Hints that the buffer may be *partially* emitted (see [emit]) to the underlying sink.
     * The underlying sink will not be explicitly flushed.
     * There are no guarantees that this call will cause emit of buffered data as well as
     * there are no guarantees how many bytes will be emitted.
     *
     * Typically, application code will not need to call this: it is only necessary when
     * application code writes directly to this [buffered].
     * Use this to limit the memory held in the buffer.
     *
     * Consider using [Sink.writeToInternalBuffer] for writes into [buffered] followed by [hintEmit] call.
     *
     * @throws IllegalStateException when the sink is closed.
     * @throws IOException when some I/O error occurs.
     */
    @InternalIoApi
     fun hintEmit() = srcSink.hintEmit()

}